package com.ruoyi.web.controller.finance;



import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.finance.domain.DTO.KltPurchaseBatchDTO;
import com.ruoyi.finance.domain.KltPurchaseBatch;
import com.ruoyi.finance.domain.VO.KltPurchaseBatchVO;
import com.ruoyi.finance.service.KltPurchaseBatchService;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.PageUtils.startPage;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * (KltPurchaseBatch)表控制层
 *
 * @author makejava
 * @since 2024-12-19 12:18:03
 */
@RestController
@RequestMapping("finance/kltPurchaseBatch")
public class KltPurchaseBatchController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private KltPurchaseBatchService kltPurchaseBatchService;

    /**
     * 分页查询所有数据
     *
     * @param kltPurchaseBatch 分页对象
     * @param kltPurchaseBatch 查询实体
     * @return 所有数据
     */

    @GetMapping("/list")
    public AjaxResult selectAll(Page<KltPurchaseBatch> page, KltPurchaseBatch kltPurchaseBatch,Integer salesOrderId) {
        if(salesOrderId!=null){
            startPage();
            List<KltPurchaseBatch> kltPurchaseBatches = kltPurchaseBatchService.selectBySales(salesOrderId);
            return success(getDataTable(kltPurchaseBatches));
        }else{
            Page<KltPurchaseBatch> page1 = this.kltPurchaseBatchService.page(page, new QueryWrapper<>(kltPurchaseBatch));
            return success(page1);
        }
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return success(this.kltPurchaseBatchService.getById(id));
    }
    @GetMapping("/selectAll")
    public AjaxResult selectAll( KltPurchaseBatch kltPurchaseBatch)
    {
        return success(kltPurchaseBatchService.getAllKltPurchaseBatch(new QueryWrapper<>(kltPurchaseBatch)));
    }
    /**
     * 新增数据
     *
     * @param kltPurchaseBatch 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody KltPurchaseBatchVO kltPurchaseBatchvo) {
        KltPurchaseBatch kltPurchaseBatch = new KltPurchaseBatch();
        BeanUtils.copyBeanProp(kltPurchaseBatch,kltPurchaseBatchvo);
        Boolean save = this.kltPurchaseBatchService.save(kltPurchaseBatch);
        Boolean b = kltPurchaseBatchService.InsertPurchaseBatchSalesOrder(kltPurchaseBatch.getPurchaseId(),kltPurchaseBatchvo.getSalesOrderId());

        return success(save||b);
    }

    /**
     * 修改数据
     *
     * @param kltPurchaseBatch 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody KltPurchaseBatch kltPurchaseBatch) {
        return success(this.kltPurchaseBatchService.updateById(kltPurchaseBatch));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return success(this.kltPurchaseBatchService.removeByIds(idList));
    }

    @GetMapping("count")
    public AjaxResult count( KltPurchaseBatch kltPurchaseBatch) {
        long count = this.kltPurchaseBatchService.count(new QueryWrapper<>(kltPurchaseBatch));
        System.out.println(count);
        return success(count);
    }
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<KltPurchaseBatchDTO> util = new ExcelUtil<KltPurchaseBatchDTO>(KltPurchaseBatchDTO.class);
        List<KltPurchaseBatchDTO> kltPurchaseBatch = util.importExcel(file.getInputStream());
        String operName = getUsername();
        List<KltPurchaseBatch> kltPurchaseBatchlist = new ArrayList<>();
        for (KltPurchaseBatchDTO purchaseBatch : kltPurchaseBatch) {
            KltPurchaseBatch kltPurchaseBatch1 = new KltPurchaseBatch();
            kltPurchaseBatch1.setPurchaser(purchaseBatch.getPurchaser());
            kltPurchaseBatch1.setPurchaseDate(purchaseBatch.getPurchaseDate());
            kltPurchaseBatch1.setOrderName(purchaseBatch.getOrderName());
            kltPurchaseBatch1.setOrderNumber(purchaseBatch.getOrderNumber());

            kltPurchaseBatch1.setCreateSource(operName);
            kltPurchaseBatch1.setUpdateUser(operName);
            kltPurchaseBatchlist.add(kltPurchaseBatch1);
        }
        boolean b = kltPurchaseBatchService.saveBatch(kltPurchaseBatchlist);
        String message="";
        if(b){
             message = "导入成功";
        }
        return success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<KltPurchaseBatchDTO> util = new ExcelUtil<KltPurchaseBatchDTO>(KltPurchaseBatchDTO.class);
        util.importTemplateExcel(response, "采购批次");
    }
    @PostMapping("/export")
    public void export(HttpServletResponse response,  KltPurchaseBatch kltPurchaseBatch)
    {
        List<KltPurchaseBatch> list = kltPurchaseBatchService.getAllKltPurchaseBatch(new QueryWrapper<>(kltPurchaseBatch));
        List<KltPurchaseBatchDTO> list1 = new ArrayList<>();
        list.forEach(PurchaseBatch->{
            KltPurchaseBatchDTO kltPurchaseBatch1 = new KltPurchaseBatchDTO();
            kltPurchaseBatch1.setPurchaser(PurchaseBatch.getPurchaser());
            kltPurchaseBatch1.setPurchaseDate(PurchaseBatch.getPurchaseDate());
            kltPurchaseBatch1.setOrderNumber(PurchaseBatch.getOrderNumber());
            kltPurchaseBatch1.setOrderName(PurchaseBatch.getOrderName());

            list1.add(kltPurchaseBatch1);
        });
        ExcelUtil<KltPurchaseBatchDTO> util = new ExcelUtil<KltPurchaseBatchDTO>(KltPurchaseBatchDTO.class);
        util.exportExcel(response,list1,"采购批次");
    }




}



